<?php
class scaffolding_administration
    {
    public $xmlDatabase;
    public $formClass = "snapol_form";
    public $tableClass = "snapol_table";
    public $tablePaging = 20;

    public $errorClass = "snapol_error_message";

    public $tableUrl;
    public $formUrl;

    public $xmlFilename;
    public $baseDir = false;


    /**
      * erstellt das Backend
      * 
      */
    //$xmlFilename, $baseDir = false,
    function createAdministration()
        {
        $addModificationTime=false;

        // Admin Ãœbersicht Page erstellen
        $content=new content();
        $content->title="Admin";
        $content->url="/admin";
        $content->template="admin.html";
        $content->usergroup=0;
        $content->usergroupEdit=99;
        $content->text=
            '<ul com="menu" node_url="/admin">
            <li com="menu_inactive"><a href="[menu.url]">[menu.title]</a></li>
            <li com="menu_active">[menu.title]</li>
        </ul>';
        $content->createPage();


        // Xml Datenbank einlesen
        if ($this->xmlDatabase == NULL)
            {
            $this->xmlDatabase=new xml_database();
            $this->xmlDatabase->baseDir=$this->baseDir;
            $this->xmlDatabase->xmlFilename=$this->xmlFilename;

            $this->xmlDatabase->readXmlFile();
            }

        foreach ($this->xmlDatabase->xmlTable as $xmlTable)
            {
            if ($xmlTable->scaffolding)
                {
                scaffolding_administration::createBackend($xmlTable->tableName);
                }
            }


        // Speichern des Modification Datum
        if ($addModificationTime)
            {
            $data=array();

            $data['modus']="db";
            $data['filename']=$xmlFilename;
            $data['_datetime']="";
            db::saveData("_modification", $data);
            }
        }

    function createBackend($table)
        {


        /*       $this->xmlDatabase=new xml_database();
          $this->xmlDatabase->baseDir=false;
          $this->xmlDatabase->readXmlPath(config::$projectPath . "/db");*/

        // Xml Datenbank einlesen
        if ($this->xmlDatabase == NULL)
            {
            $this->xmlDatabase=new xml_database();
            $this->xmlDatabase->baseDir=$this->baseDir;
            $this->xmlDatabase->xmlFilename=$this->xmlFilename;

            $this->xmlDatabase->readXmlFile();
            }

        $xmlTable=$this->xmlDatabase->getTable($table);

        if ($xmlTable->scaffolding)
            {


            //$caption = $table;
            $usergroup=$xmlTable->usergroup;

            if ($usergroup == "")
                {
                $usergroup=10;
                }

            $this->tableUrl="/admin" . convert::convertToUrl($table);
            $this->formUrl="/admin/neu" . convert::convertToUrl($table);


            // Formular erstellen
            $htmlForm=$this->createForm($table);

            // Form erstellen
            $content=new content();
            $content->title="Neuer Eintrag ({$xmlTable->tableCaption})";
            $content->url=$this->formUrl;
            $content->template="admin.html";
            $content->usergroup=$usergroup;
            $content->usergroupEdit=99;
            $content->parentUrl=$this->tableUrl;
            //$content->distributionPath='C:\xampp\htdocs';
            $content->text=$htmlForm;
            $content->systemPath="/";
            $content->createPage();


            // Tabelle erstellen

            $htmlTable=$this->createTable($table);

            // Tabelle erstellen
            $content=new content();
            $content->title=$xmlTable->tableCaption; //$caption; // . " Administration";
            $content->url=$this->tableUrl;
            $content->template="admin.html";
            $content->usergroup=$usergroup;
            $content->usergroupEdit=99;
            $content->parentUrl="/admin";
            $content->text=$htmlTable;
            $content->systemPath="/";

            $content->createPage();
            }
        }


    /**
    * erstellt ein Formular
    * 
    */
    function createForm($table)
        {

        //echo "tabelle: $table <br>";

        $htmlForm="";
        $searchIndexModus=false;
        $tableUrl="";

        // Xml Datenbank einlesen
        if ($this->xmlDatabase == NULL)
            {
            $this->xmlDatabase=new xml_database();
            $this->xmlDatabase->baseDir=$this->baseDir;
            $this->xmlDatabase->xmlFilename=$this->xmlFilename;

            $this->xmlDatabase->readXmlFile();
            }

        $xmlTable=$this->xmlDatabase->getTable($table);


        // print_r($xmlTable);
        //foreach ($this->xmlDatabase->xmlTable as $table)
        //  {
        $counter=0;


        // scaffolding
        /*   if ($table->scaffolding)
               {
               $tableUrl        ="/admin" . convert::convertToUrl($table->tableName);
               $formUrl         ="/admin/neu" . convert::convertToUrl($table->tableName);

               $htmlForm        ='';
               //$tableArray = general::stringToArray()

               $searchIndexModus=false; */

        foreach ($xmlTable->xmlField as $field)
            {


            // falls required_text, Fehlermeldung einbinden
            //         $fieldTitle = $field->fieldCaption;


            // $html = $html.$fieldTitle;


            // falls required_text, Fehlermeldung einbinden
            $fieldTitle = $field->fieldCaption;

            if ($field->required)
                {
                $fieldTitle=$fieldTitle . " <b>[error:" . strtolower($field->fieldName) . "]</b>";
                }

            if ($field->searchIndex)
                {
                $searchIndexModus=true;
                }


            // Text Component
            if (($field->fieldType == "text") or ($field->fieldType == "time"))
                {
                
                    
                $filename = config::$projectPath . "/scaffolding/text_input.html";    
                if (file::fileExists($filename, false))
                    {

                    $itemHtml=file::readTextFile($filename, false);
                    }
                else
                    {
 
                    $itemHtml='<p><label>[caption] <span com="error" field="[name]" class="">Keine gÃ¼ltige Eingabe</span></label>
  <input type="text" name="[name]" id="[name]" /></p>';
                    }

  
                    
                //$itemHtml=dom::setAttribute("input", "name", $field->fieldName, $itemHtml);
                //$itemHtml=dom::setAttribute("input", "id", $field->fieldName, $itemHtml);
                $itemHtml=str_ireplace("[caption]", $field->fieldCaption, $itemHtml);
                $itemHtml=str_ireplace("[name]", $field->fieldName, $itemHtml);
                
             
                

                if ($field->required)
                    {    
                        $itemHtml=dom::setAttribute("input", "check_content", "true", $itemHtml);   
                    }
                
                
                
                // falls required_text, Fehlermeldung einbinden
               /* $check="";
                $fieldTitle=$field->fieldCaption;

                if ($field->required)
                    {
                    $check='check_content="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Keine gÃ¼ltige Eingabe</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <input type="text" name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check . ' /></p>';*/
  
                $text = $itemHtml;
                
                
                
                
                
                }

            // eMail Component
            if ($field->fieldType == "email")
                {


                // falls required_text, Fehlermeldung einbinden
                $check="";
                $fieldTitle=$field->fieldCaption;

                if ($field->required)
                    {
                    $check='check_email="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Keine gÃ¼ltige eMail Adresse</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <input type="text" name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check . ' /></p>';
                }

            // Url Component
            if ($field->fieldType == "url")
                {


                // falls required_text, Fehlermeldung einbinden
                $check="";

                if ($field->required)
                    {
                    $check='check_url="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Keine gÃ¼ltige Url</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <input type="text" name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check . ' /></p>';
                }

            // Number Component
            if ($field->fieldType == "number")
                {


                // falls required_text, Fehlermeldung einbinden
                $check="";

                if ($field->required)
                    {
                    $check='check_number="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Keine gÃ¼ltige Zahl</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <input type="text" name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check . ' /></p>';
                }


            // Rational Component
            if ($field->fieldType == "rational_number")
                {


                // falls required_text, Fehlermeldung einbinden
                $check="";

                if ($field->required)
                    {
                    $check='check_number="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Keine gÃ¼ltige Zahl</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <input type="text" name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check . ' /></p>';
                }


            // Text Area Component
            if ($field->fieldType == "long_text")
                {


                // falls required_text, Fehlermeldung einbinden
                $check="";

                if ($field->required)
                    {
                    $check='check_content="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Fehlende Eingabe</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <textarea name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check
                    . ' cols="60" rows="10"></textarea></p>';
                }


            // falls Text Area
            if ($field->fieldType == "html")
                {
                $check="";

                if ($field->required)
                    {
                    $check='check_content="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Fehlende Eingabe</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <textarea com="ck_editor" name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check
                    . ' cols="60" rows="10"></textarea></p>';
                }


            // falls Datum
            if ($field->fieldType == "date")
                {


                // falls required_text, Fehlermeldung einbinden
                $check="";

                if ($field->required)
                    {
                    $check='check_content="true"';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <input type="text" date_picker="true" name="' . $field->fieldName . '" id="' . $field->fieldName . '" ' . $check
                    . ' /></p>';
                }


            // falls Checkbox
            if ($field->fieldType == "checkbox")
                {
                $text='<p>
  <label><input type="checkbox" name="' . $field->fieldName . '" id="' . $field->fieldName . '"  />'
                    . $field->fieldCaption . '</label></p>';
                }


            // falls Image File
            if ($field->fieldType == "image")
                {
                $text='<p><img src="[system.base_url]/code/upload/' . $xmlTable->tableName . '/100/[form_view:' . $field->fieldName . ']" /></p><p><label>' . $fieldTitle . '</label>
  <input type="file"  name="' . $field->fieldName . '" id="' . $field->fieldName . '" /></p>';
                }


            // falls File
            if ($field->fieldType == "file")
                {
                if ($field->required)
                    {
                    $check='check_content="true"';
                    $fieldTitle=$field->fieldCaption . ' <span com="error" field="' . $field->fieldName . '" class="'
                        . $this->errorClass . '">Keine Datei angegeben</span>';
                    }

                $text='<p><label>' . $fieldTitle . '</label>
  <input type="file"  name="' . $field->fieldName . '" id="' . $field->fieldName . '" /></p>';
                ;
                }


            // falls Select
            if ($field->fieldType == "join")
                {
                
                    
                    
                    
                    
                    
                    
                $filename = config::$projectPath . "/scaffolding/select.html";    
                if (file::fileExists($filename, false))
                    {

                    $itemHtml=file::readTextFile($filename, false);
                    }
                else
                    {
 
                    /*$itemHtml='<p><label>[caption] <span com="error" field="[name]" class="">Keine gÃ¼ltige Eingabe</span></label>
  <input type="text" name="" id="" /></p>';*/
  
  
  
                    $itemHtml='<label>[caption]</label>       
                        <select name="[name]" id="[name]" com="data_select" table="[select_table]" field="[join_field]">
                        </select>';
  
                    }    
                    
                    
                    $selectTable=$field->fieldName;
                $selectTable=substr($selectTable, 1);


                //$joinTable=substr($value, 1);
                $joinTable=substr($field->fieldName, 1);

                $optionField="";
                //if ($this->joinField !== "") {

                foreach (general::stringToArray($field->joinField) as $joinFieldItem)
                    {
                    $optionField=$optionField . "[$joinFieldItem] ";

                    //echo "item=$joinFieldItem***";
                    }


                // falls nichts definiert wurde
                if ($optionField === "")
                    {
                    $optionField="[$selectTable]";
                    }
                    
                    
                    
                    
                    
                    $itemHtml=str_ireplace("[caption]", $fieldTitle=$field->fieldCaption, $itemHtml);
                $itemHtml=str_ireplace("[name]", $fieldTitle=$field->fieldName, $itemHtml);
                 $itemHtml=str_ireplace("[select_table]", $selectTable, $itemHtml);  
                 $itemHtml=str_ireplace("[join_field]", $field->joinField, $itemHtml);  



                 
                 
                if ($field->required)
                    {    
                        $itemHtml=dom::setAttribute("input", "check_content", "true", $itemHtml);   
                    }
                    
                    
                    
                    
                

/*$text='<label>' . $fieldTitle . '</label>
<select name="' . $field->fieldName . '" id="' . $field->fieldName . '" com="data" table="' . $selectTable . '" orderby="" limit="">
<option value="[id]">' . $optionField . '</option>
</select>';*/

          /*      $text='<label>' . $fieldTitle . '</label>       
                        <select name="' . $field->fieldName . '" id="' . $field->fieldName . '" com="data_select" table="' . $selectTable . '" field="' . $field->joinField . '">
                        </select>'; */
                        
                        
                        $text = $itemHtml;
                        
                      //  echo $text;
                      //  exit;
                
                
                
                }


            // falls required_text, Fehlermeldung einbinden
            /*if ($field->required)
                {
                $title=$title . " <b>[error:$field]</b>";
                }

            //$text=dom::replaceInnerHtml("label", $title, $text);

            //print_r($comData);
            //exit;

            //if ($com == "required_text")
            /* wieder aktivieren     if ($com == "text") // and ($comData['required'] == 1))
                     {
     
                     //$text = dom::replaceInnerHtml("label", $dataForm->data['field']." [error:{$dataForm->data['field']}]",$text);
                     $text=dom::replaceInnerHtml("label",
                                                 $title . ' <span com="error" field="' . $value
                                                     . '" error_message="uups, es gab einen Fehler!!!">Error Message</span>',
                                                 $text);
                     $text=dom::setAttribute("input", "check_content", "on", $text);
                     $text=dom::setAttribute("input", "check_content_error_message", "uups, hier gabs einen fehler.", $text);
                     }
     
                 // falls required_email
                 //if ($com == "required_email")
                 if (($com == "email") and ($comData['required'] == 1))    
                     {
                     $text=dom::replaceInnerHtml("label",
                                                 $title . ' <span com="error" field="' . $value
                                                     . '" error_message="Keine gÃƒÆ’Ã†â€™Ãƒâ€šÃ‚Â¼ltige eMail Adresse.">Error Message</span>',
                                                 $text);
                     $text=dom::setAttribute("input", "check_email", "on", $text);
                     }  */


            // Set Focus setzen (beim ersten Element)
            if ($counter == 0)
                {


                //wieder aktivieren problem mit php und html header!!!!
                $text=dom::setAttribute("input", "set_focus", "true", $text);
                }


            //echo $text;

            $htmlForm=$htmlForm . $text;

            $counter++;
            //   } while ($dataForm->nextRow());
            }

        //$text=component::getCom("input_submit");
        //$text = $replaceCom->getComData("input_submit");
        //$comData = $replaceCom->getComData("input_submit");

        // Submit Button anfÃƒÆ’Ã‚Â¼gen
        //$comData=component::getCom("input_submit");

        //$text=$comData['body'];

        /*$text                     ='<p><label></label>  
<input type="submit" name="" value="Speichern" /></p>'; */

        $searchIndex="";

        if ($searchIndexModus)
            {
            $searchIndex=' search_index="true"';
            }

        $urlTable="";

        if ($xmlTable->urlTable)
            {

            $urlTable=' url_table="true" url_field="' . $table->urlField . '"';
            }

        $htmlForm='<form class="' . $this->formClass . '" action="' . $this->tableUrl . '" com="save_form" table="'
            . $xmlTable->tableName . '"' . $searchIndex . $urlTable . ' update_id="[get:id]">' . $htmlForm;

        $htmlForm=$htmlForm . '<p><label></label>  
  <input type="submit" name="" value="Speichern" /></p></form>';

        return $htmlForm;
        }

    function createTable($table)
        {


        /*       $this->xmlDatabase=new xml_database();
       $this->xmlDatabase->baseDir=false;
       $this->xmlDatabase->readXmlPath(config::$projectPath . "/db");*/

        if ($this->xmlDatabase == NULL)
            {
            $this->xmlDatabase=new xml_database();
            $this->xmlDatabase->baseDir=$this->baseDir;
            $this->xmlDatabase->xmlFilename=$this->xmlFilename;

            $this->xmlDatabase->readXmlFile();
            }

        $xmlTable=$this->xmlDatabase->getTable($table);

        $headerField="";
        $bodyField="";

        $header="";
        $body="";

        $joinTable="";


        /*if ($this->tableField == "")
            {
            $field=db::getSnapolTableFieldArray($this->table);
            }
        else
            {
            $field=general::stringToArray($tableField);
            }*/


        //$fieldTitle = general::stringToArray($this->tableFieldTitle);

        $n=0;

        foreach ($xmlTable->xmlField as $field)
            {

            //$type = $this->tableFieldType[$field];
            //$title=$value;


            // falls es angezeigt werden soll
            if ($field->tableVisible)
                {


                //$headerField=$value;  //$fieldTitle[$n];
                $headerField=$field->fieldCaption;
                $bodyField="[" . $field->fieldName . "]";

                // falls Feld ein Search Index hat
                if ($field->searchIndex)
                    {
                    $bodyField="[" . $field->fieldName . ":bold]";
                    }

                if ($field->fieldType == "image")
                    {
                    $headerField="&nbsp;";
                    $bodyField='<img src="[' . $field->fieldName . ':100]" />';
                    }

                if ($field->fieldType == "file")
                    {
                    $bodyField='<a href="[' . $field->fieldName . ']" target="_blank">[' . $field->fieldName
                        . ':filename]</a>';
                    }

                if ($field->fieldType == "email")
                    {
                    $bodyField='<a href="mailto:[' . $field->fieldName . ']">[' . $field->fieldName . ']</a>';
                    }

                if ($field->fieldType == "number")
                    {
                    $bodyField="[" . $field->fieldName . ":number]";
                    }

                if ($field->fieldType == "url")
                    {
                    $bodyField='<a href="[' . $field->fieldName . ']" target="_blank">[' . $field->fieldName . ']</a>';
                    }

                if ($field->fieldType == "date")
                    {
                    $bodyField="[" . $field->fieldName . ":short_date]";
                    }

                if ($field->fieldType == "join")
                    {
                    //$headerField=$joinTable;
                    $headerField=$field->fieldCaption;

                    //$bodyField="[" . $joinTable . "]";

                    $optionField="";
                    //if ($this->joinField !== "") {

                    foreach (general::stringToArray($field->joinField) as $joinFieldItem)
                        {
                        $optionField=$optionField . "[$joinFieldItem] ";
                        }


                    // falls nichts definiert wurde
                    if ($optionField === "")
                        {
                        $optionField="[" . substr($field->fieldName, 1) . "]";
                        }

                    $joinTable="$joinTable," . substr($field->fieldName, 1);
                    $bodyField=$optionField;
                    }


                //$header = $header . "<th>$value</th>";
                if ($headerField !== "")
                    {
                    $header=$header . "<th>$headerField</th>";
                    $body=$body . "<td>$bodyField</td>";
                    }

                $n++;
                }
            }

        //    if ($this->editItem)
        //      {
        $header=$header . "<th>&nbsp;</th>";
        //$body  =$body . "<td>" . $this->addDetailView() . "</td>";                    //this->tableArray[0]
        $body=$body . '<td><a href="[system.base_url]' . $this->formUrl . '?id=[' . $xmlTable->tableName
            . '_id]">editieren</a></td>';
        //    }                                       //[base_url]

        // if ($this->deleteItem)
        //   {
        $header=$header . "<th>&nbsp;</th>";
//$body  =$body . "<td>" . $this->addDetele() . "</td>";                                   //$this->tableArray[0]
        $body=$body . '<td><a href="" click="delete_data" table="' . $xmlTable->tableName . '" id="['
            . $xmlTable->tableName . '_id]">löschen</a></td>';
        // }

        // Search Field
        $searchProperty="";

        if ($xmlTable->searchForm)
            {
            $searchProperty="[get:q]";
            }

        if ($xmlTable->sortableTable)
            {
            $component="data_sortable";
            }
        else
            {
            $component="data";
            }

        $htmlTable='<table class="' . $this->tableClass . '"><thead><tr>' . $header
            . '</tr></thead><tbody id="admin_table" com="' . $component . '" table="' . $xmlTable->tableName
            . $joinTable . '" where="" orderby="' . $xmlTable->tableOrderby . '" search="' . $searchProperty
            . '" paging="' . $this->tablePaging . '">';

        if ($xmlTable->sortableTable)
            {
            $htmlTable=$htmlTable . '<tr id="[id]">';
            }
        else
            {
            $htmlTable=$htmlTable . '<tr>';
            }

        $htmlTable=$htmlTable . $body . '</tr></tbody></table>';


        // Paging anhÃ¤ngen (bei sortable soll es kein Paging geben)
        if (!$xmlTable->sortableTable)
            {
            $htmlTable=$htmlTable . '<div com="paging" table="' . $xmlTable->tableName . '" paging="'
                . $this->tablePaging . '" show_paging="true">Paging</div>';
            }


        // anzeigen
        if ($xmlTable->searchForm)
            {
            $htmlTable='<form id="form1" name="form1" method="get" action="">
          <label for="q"></label>
          <input type="text" name="q" id="q" value="[get:q]" />
          <input type="submit" name="button" id="button" value="Search" />
        </form>'
                . $htmlTable;
            }

        //if ($this->newItem)
        //  {                                                      //[base_url]
        $htmlTable='<p><a class="snapol_button" href="[system.base_url]' . $this->formUrl . '">Neuer Eintrag</a></p>'
            . $htmlTable;

        return $htmlTable;
        }
    }
?>
